//
//  EpointGetAddressBookGroup.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-3-10.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointGetAddressBookGroup.h"
#import "EpointAddressGroupModel.h"

@implementation EpointGetAddressBookGroup

-(void)execute
{
    NSString *userguid = [DBFrameService getUserGuid];
    NSString *urlstr = [EpointBizTools getUrl:[NSString stringWithFormat:@"interface.jspx?mbtype=com.epoint.mobile.httpbiz.MobileCommonService;getMyAddressGroup&userguid=%@",userguid]];
    NSString *bs = [HttpUtil getZipStr:urlstr];
    
    NSMutableArray *dataArr = [self getLst:bs];
    if (dataArr.count>0) {
        NSMutableArray *listArr = [[NSMutableArray alloc]init];
        for (int i=0; i<dataArr.count; i++) {
            EpointAddressGroupModel *m = [dataArr objectAtIndex:i];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setObject:m.groupname forKey:@"name"];
            [dic setObject:m.groupguid forKey:@"guid"];
            [dic setObject:@"ou" forKey:@"type"];
            [listArr addObject:dic];
        }
        [self exeSuccessWithData:listArr];
    }
    else{
        [self exeFailureWithMessage:@"获取失败！"];
    }
}

-(NSMutableArray *)getLst:(NSString *)bs
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"GroupList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Group"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointAddressGroupModel *m = [[EpointAddressGroupModel alloc]init];
        GDataXMLElement *groupguid = [[arr elementsForName:@"groupguid"] objectAtIndex:0];
        m.groupguid = [groupguid stringValue];
        
        GDataXMLElement *groupname = [[arr elementsForName:@"groupname"] objectAtIndex:0];
        m.groupname = [groupname stringValue];
        
        [listmp addObject:m];
    }
    
    return listmp;
}

@end
